<template>
  <el-dialog title="分配员工角色" :visible="isShowDialog" @close="btnCancel">
    <el-checkbox-group v-model="checkList">
      <el-checkbox
        v-for="item in roleList"
        :key="item.id"
        :label="item.id"
      >
        {{ item.name }}
      </el-checkbox>
    </el-checkbox-group>
    <template #footer>
      <el-button type="primary" @click="btnOK">确定</el-button>
      <el-button @click="btnCancel">取消</el-button>
    </template>
  </el-dialog>
</template>

<script>
import { getRoleList } from '@/api/setting'
import { getUserDetail } from '@/api/user'
import { assignRole } from '@/api/employee'
export default {
  props: {
    isShowDialog: {
      type: Boolean,
      required: true,
      default: false
    }
  },
  data() {
    return {
      // 选中状态的角色
      checkList: [],
      // 所有角色
      roleList: [],
      userId: ''
    }
  },
  async created() {
    // 传pagesize999是为了避免角色过多,导致无法全部显示
    const res = await getRoleList({
      page: 1,
      pagesize: 9999
    })
    // console.log(res)
    this.roleList = res.rows
  },
  methods: {
    // 获取角色详细信息
    async getUserDetail(id) {
      const res = await getUserDetail(id)
      this.userId = id
      // 给到checkList以渲染到弹窗中
      this.checkList = res.roleIds
    },
    // 确认按钮
    async  btnOK() {
      await assignRole({
        // 需要传入点击时传的id,以确认修改哪位员工
        id: this.userId,
        roleIds: this.checkList
      })
      this.$message.success('操作成功')
      this.$parent.isShowRole = false
    },
    // 取消
    btnCancel() {
      this.checkList = []
      this.$parent.isShowRole = false
    }
  }
}
</script>

<style>

</style>
